package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.dto.DeviceDto;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备Controller
 * 
 * @author ruoyi
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 1、从物联网平台同步产品列表 - Day9 随堂1
     * @return
     */
    @PostMapping("syncProductList")
    @ApiOperation(value = "同步产品列表")
    public R syncProductList() {
        this.deviceService.syncProductList();
        return R.ok();
    }

    /**
     * 2、查询所有产品列表 Day9 随堂2
     * @return
     */
    @GetMapping("allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public R<List<ProductVo>> allProduct(){
        return R.ok(this.deviceService.allProduct());
    }

    /**
     * 3、注册设备 Day9 随堂3
     * @param device
     * @return
     */
    @ApiOperation(value = "注册设备")
    @PostMapping("register")
    public R registerDevice(@RequestBody DeviceDto device) {
        this.deviceService.registerDevice(device);
        return R.ok();
    }


    /**
     * 4、查询设备列表 Day9 随堂4
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 5、查询设备详情 - Day9 作业1
     * @param deviceId iot设备ID
     * @return
     */
    @GetMapping("/{deviceId}")
    @ApiOperation("查询设备详情")
    public R<DeviceDetailVo> queryDeviceDetailInfo(@ApiParam(value = "物联网设备ID",required = true) @PathVariable("deviceId") String deviceId){
        return R.ok(this.deviceService.queryDeviceDetailInfo(deviceId));
    }

    /**
     * 6、查询设备最近一次上报数据 Day9 作业2
     * @param deviceId iot设备ID
     * @return
     */
    @GetMapping("/queryServiceProperties/{deviceId}")
    @ApiOperation("查询设备最近一次上报数据")
    public R<List> queryServiceProperties(@ApiParam(value = "物联网设备ID",required = true) @PathVariable("deviceId") String deviceId){
        return R.ok(this.deviceService.queryServiceProperties(deviceId));
    }

    /**
     * 7、修改设备信息 - Day9 作业3
     * @return
     */
    @PutMapping
    @ApiOperation("修改设备信息")
    public R edit(@RequestBody DeviceDto deviceDto){
        this.deviceService.updateDevice(deviceDto);
        return R.ok();
    }

    /**
     * 8、删除设备 - Day9 作业4
     * @param deviceId iot设备ID
     * @return
     */
    @DeleteMapping("{deviceId}")
    @ApiOperation("删除设备")
    public R delete(@ApiParam(value = "物联网设备ID", required = true) @PathVariable("deviceId") String deviceId){
        this.deviceService.deleteDevice(deviceId);
        return R.ok();
    }
}
